
//-------------------------------------------------------------
//-------------------------------------------------------------
group_add.Instances = null;
//-------------------------------------------------------------
group_add.CreateObject = function(config) {
    var obj = null;
    if (group_add.Instances != null) {
        obj = group_add.Instances[config.id];
    }
    if (obj == null) {
        if (group_add.Instances == null) {
            group_add.Instances = new Object();
        }
        obj = new group_add(config);
        group_add.Instances[config.id] = obj;
    }
    obj.init();

    return obj;
};
//-------------------------------------------------------------
function group_add(config) {
    var Me = this;
    this.id = config.id;
    this.FOLDER_UPLOAD_TEMP = config.FOLDER_UPLOAD_TEMP;


    //------------------------------------------------------------
    this.classAddGroup_click = function(){
        
    }
//    this.classEdit_click = function() {
//        var id = jQuery(this).attr('itemid');
//        _ajax.url = url_base + "admin/group/edit?id=" + id;
//        _ajax.loading(true);
//        _ajax.data = '';
//        _ajax.method = 'GET';
//        _ajax.success = function(data) {
//            if (data.error) {
//                show_msg_error(data.msg);
//            } else {
//                
//                jQuery(".cmdCancelEdit").unbind('click').click(function() {
//                    Me.back_list();
//                });
//            }
//            _ajax.loading(false);
//        }
//        _ajax.show();
//    }
    this.init = function() {
//        $('[data-rel=tooltip]').tooltip();
//
//        $(".select2").css('width', '200px').select2({allowClear: true})
//                .on('change', function() {
//                    $(this).closest('form').validate().element($(this));
//                });

//        $.mask.definitions['~'] = '[+-]';

        jQuery.validator.addMethod("Group", function(value, element) {

            return true;
        }, "This field is required.");

        var fb = true;
        $('#validation-form').validate({
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: false,
            rules: {
                namegroup: {
                    required: true
                },
                description: {
                    required: true
                },
                location_id: {
                    required: true
                },
                permission_id: {
                    required: true
                },
            },
            messages: {
            },
            invalidHandler: function(event, validator) { //display error alert on form submit   
                $('.alert-danger', $('.login-form')).show();
            },
            highlight: function(e) {
                if (fb) {
                    $(e).focus();
                    fb = false;
                }
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success: function(e) {
                $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
                $(e).remove();

            },
//            errorPlacement: function(error, element) {
//                if (element.is(':checkbox') || element.is(':radio')) {
//                    var controls = element.closest('div[class*="col-"]');
//                    if (controls.find(':checkbox,:radio').length > 1)
//                        controls.append(error);
//                    else
//                        error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
//                }
//                else if (element.is('.select2')) {
//                    error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
//                }
//                else if (element.is('.chosen-select')) {
//                    error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
//                }
//                else
//                    error.insertAfter(element.parent());
//            },
            submitHandler: function(form) {
                //save
//                var id = jQuery(this).attr('itemid');
                _ajax.url = url_base + "admin/group/savegroup";
                _ajax.data = jQuery(form).serialize();
                _ajax.loading(true);
               // jQuery('.wizard-actions button').hide();
                jQuery("#loading").show();
                _ajax.method = 'POST';
                _ajax.success = function(data) {
                    _ajax.loading(false);
                    if (data.error == 1) {
                        show_msg_error(data.msg);
                    } else {
                        
                            show_msg_success(data.msg);
                    }
                    jQuery("#loading").hide();
                };
                _ajax.show();
            },
        });
        $('#permission_id').multiSelect({
            selectableOptgroup: true
        });
        $('#selectallper').click(function() {
            $('#permission_id').multiSelect('select_all');
            return false;
        });
        $('#deselectallper').click(function() {
            $('#permission_id').multiSelect('deselect_all');
            return false;
        });
        jQuery("#cmdsavegroup").click(function() {
            fb = true;
            $('#validation-form').submit();
        });
    }


    //javascript code here
}


